package com.wcg.webmagic.login;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;



import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.utils.HttpConstant;

public class FromProcessor1 implements PageProcessor{

	static String cookie="AEB7DF41452DB303CCC86CF8CCC42DD4";
	
	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000)
						.setDomain("http://www.scholat.com")
						.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
						.addHeader("Accept-Encoding", "gzip, deflate")
						.addHeader("Accept-Language", "zh-CN,zh;q=0.9")
						.addHeader("Cache-Control","max-age=0")
						.addHeader("Connection","keep-alive")
						.addHeader("Cookie", "JSESSIONID=C5F50C6A93FCFB8C72A0F6D591190579")
//						.addHeader("Host","http://www.scholat.com")
//						.addHeader("Upgrade-Insecure-Requests","1")
//						.addHeader("Referer","http://www.scholat.com/login.html")
						.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");

    @Override
    public void process(Page page) {
    	if(!page.getUrl().regex("http://localhost:8080/posp-boss/login.jsp").match()){
 //   		page.getRequest().get
    	
    	}
    	
        page.putField("netname", page.getHtml().xpath("//div[@id='divheaderinfo_left']/a/text()"));
    }
    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
    	double random = Math.random();
    	Spider.create(new FromProcessor1()).addUrl("http://localhost:8080/posp-boss/login.jsp").thread(1).run();
    	
    	Map<String, Object> fromMap = new HashMap<String,Object>();
    	fromMap.put("branchno", "");
    	fromMap.put("username", "admin");
    	fromMap.put("password", "123456");
    	fromMap.put("checkcode", "");
    	
    	
    	
    	Request request = new Request("http://localhost:8080/posp-boss/userLogin.html");
		try {
			request.setMethod(HttpConstant.Method.POST);
			request.setRequestBody(HttpRequestBody.form(fromMap, "utf-8"));
//			request.addCookie("JSESSIONID", cookie);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
        Spider.create(new FromProcessor1()).addRequest(request).thread(1).run();
    }
}
